<template>
  <div class="announcements-page">
    <div class="content-wrapper">
      <h1 class="page-title">公告列表</h1>
      
      <el-card class="announcement-list">
        <div
          v-for="item in announcements"
          :key="item.id"
          class="announcement-item"
          @click="handleAnnouncementClick(item)"
        >
          <div class="title">
            <el-icon><Bell /></el-icon>
            {{ item.title }}
          </div>
          <div class="meta">
            <span class="time">{{ formatDate(item.createTime) }}</span>
          </div>
        </div>
      </el-card>

      <!-- 分页 -->
      <div class="pagination-container">
        <el-pagination
          v-model:current-page="currentPage"
          v-model:page-size="pageSize"
          :total="total"
          :page-sizes="[10, 20, 30]"
          layout="total, sizes, prev, pager, next"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import { useRouter } from 'vue-router'
import { Bell } from '@element-plus/icons-vue'
import { getList } from '@/api/announcement'

const router = useRouter()
const announcements = ref([])
const currentPage = ref(1)
const pageSize = ref(10)
const total = ref(0)

const formatDate = (dateStr) => {
  if (!dateStr) return ''
  return new Date(dateStr).toLocaleString()
}

const loadAnnouncements = async () => {
  try {
    const res = await getList({
      page: currentPage.value,
      size: pageSize.value
    })
    if (res.code === 200) {
      announcements.value = res.data.records
      total.value = res.data.total
    }
  } catch (error) {
    console.error('加载公告失败:', error)
  }
}

const handleAnnouncementClick = (announcement) => {
  router.push(`/front/announcements/${announcement.id}`)
}

const handleSizeChange = (val) => {
  pageSize.value = val
  loadAnnouncements()
}

const handleCurrentChange = (val) => {
  currentPage.value = val
  loadAnnouncements()
}

onMounted(() => {
  loadAnnouncements()
})
</script>

<style lang="scss" scoped>
.announcements-page {
  .content-wrapper {
    max-width: 800px;
    margin: 0 auto;
    padding: 20px;
  }

  .page-title {
    font-size: 28px;
    color: #303133;
    margin-bottom: 30px;
    text-align: center;
  }

  .announcement-list {
    .announcement-item {
      padding: 20px;
      cursor: pointer;
      transition: all 0.3s;
      border-bottom: 1px solid #ebeef5;

      &:last-child {
        border-bottom: none;
      }

      &:hover {
        background-color: #f5f7fa;
      }

      .title {
        display: flex;
        align-items: center;
        gap: 8px;
        font-size: 16px;
        color: #303133;
        margin-bottom: 10px;

        .el-icon {
          color: #409EFF;
        }
      }

      .meta {
        color: #909399;
        font-size: 14px;
      }
    }
  }

  .pagination-container {
    margin-top: 30px;
    display: flex;
    justify-content: center;
  }
}
</style> 